package com.bestv.engine.model.topn.impl;

import com.bestv.database.vo.ItemRelation;
import com.bestv.engine.model.RecommendationModel;
import com.bestv.utils.database.DBConnectionManager;
import com.bestv.utils.database.JDBCHelper;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Henry on 14-5-20.
 */
public class LatestModel extends RecommendationModel {
    public LatestModel() {
        isRandomSort = true;
        isRandomSelect = true;
    }

    @Override
    protected List<Object> getItemRelations(long itemId, int typeId, int maxCount) {
        Connection conn = DBConnectionManager.getSlaveConnection();
        String order = isRandomSelect ? "RAND()" : "Similarity";
        String sql = String.format("SELECT LeftId,RightId,Significance AS Similarity,Similarity AS Significance FROM %s WHERE LeftId=? ORDER BY %s LIMIT ?", "modellatest", order);
        ArrayList<Object> rs = JDBCHelper.executeQuery(conn, sql, ItemRelation.class, typeId, maxCount);
        JDBCHelper.close(conn);
        return rs;
    }
}
